package pruebas;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class TestOracleXEJDBC {
	String jdbcUrl = "jdbc:oracle:thin:@juanitu:1521:XE";

	Connection conexion;

	@BeforeClass
	public void inicia() throws ClassNotFoundException, SQLException {
		System.out.println("carga de driver OK");
		Class.forName("oracle.jdbc.driver.OracleDriver");
		conexion = DriverManager.getConnection(jdbcUrl, "juan", "root");
		String sql="CREATE table \"SIETE\" (\"ID\" NUMBER, \"NOMBRE\" VARCHAR2(4000), constraint \"SIETE_PK\" primary key (\"ID\"))" ;

		conexion.createStatement().execute(sql);

		System.out.println("conexion OK");
	}

	@Test
	public void pruebaSelectLinks() throws ClassNotFoundException, SQLException {

		Statement s = conexion.createStatement();

		ResultSet rs = s.executeQuery("select * from \"SIETE\"");
		while (rs.next()) {
			System.out.println(rs.getInt(1) + " " + rs.getString(2) + " ");
		}
	}

	@Test
	public void pruebaAddLink() throws ClassNotFoundException, SQLException {

		Statement s = conexion.createStatement();

		s.execute("insert into \"SIETE\"(ID, nombre) values(1, 'nuevo')");

	}

	@AfterClass
	public void cerrar() throws SQLException {
		conexion.createStatement().execute("drop table \"SIETE\"");

		conexion.close();

	}

}
